package com.google.code.coma.space.implementation;

import com.google.code.coma.space.ComaFIFOCoordinator;
import org.mozartspaces.capi3.Capi3Exception;
import org.mozartspaces.capi3.CoordinationData;
import org.mozartspaces.capi3.CoordinatorLockedException;
import org.mozartspaces.capi3.javanative.coordination.ImplicitNativeCoordinator;
import org.mozartspaces.capi3.javanative.isolation.NativeIsolationManager;
import org.mozartspaces.capi3.javanative.isolation.NativeSubTransaction;
import org.mozartspaces.capi3.javanative.operation.NativeEntry;
import org.mozartspaces.core.RequestContext;

import java.util.Deque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author Dominik Strasser, dominikstr@gmail.com
 */
public class ComaFIFOCoordinatorImpl implements ImplicitNativeCoordinator {
	protected static final String name = "com.google.code.coma.space.ComaFIFOCoordinator";

	protected Deque<NativeEntry> queue = new LinkedBlockingDeque<NativeEntry>();

	@Override
	public CoordinationData createDefaultCoordinationData() {
		return new CoordinationData() {
			@Override
			public String getName() {
				return name;
			}
		};
	}

	@Override
	public synchronized boolean registerEntry(NativeSubTransaction stx, CoordinationData coordData, NativeEntry entry, RequestContext context) throws Capi3Exception {
		queue.add(entry);
		return true;
	}

	@Override
	public synchronized boolean unregisterEntry(NativeEntry entry, RequestContext context) {
		queue.removeFirstOccurrence(entry);
		return true;
	}

	@Override
	public void setIsolationManager(NativeIsolationManager isolationManager) {
	}

	@Override
	public void prepareEntryRemoval(NativeSubTransaction stx, NativeEntry entry, RequestContext context) throws CoordinatorLockedException {

	}

	@Override
	public String getName() {
		return name;
	}
}
